package com.example.forum.mapper;

import com.example.forum.entity.Article;
import com.example.forum.vo.ArticleDetailVO;
import com.example.forum.vo.ArticleVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author lyk19
 * @description 针对表【t_article】的数据库操作Mapper
 * @createDate 2024-08-01 12:54:53
 * @Entity generator.domain.TArticle
 */

@Mapper
public interface ArticleMapper {

    /**
     * 创建文章
     *
     * @param article
     * @return
     */
    @Insert("insert into t_article(boardId, userId, title, content, visitCount, replyCount, likeCount, state, deleteState, createTime, updateTime)" +
            "values(#{boardId}, #{userId}, #{title}, #{content}, #{visitCount}, #{replyCount}, #{likeCount}, #{state}, #{deleteState}, #{createTime}, #{updateTime})")
    Integer create(Article article);

    /**
     * 获取所有文章
     *
     * @param boardId (id可选)
     * @return
     */
    List<ArticleVO> getAllArticle(Long boardId);

    /**
     * 根据文章id获取文章
     *
     * @param articleId
     * @return
     */
    ArticleDetailVO getById(Long articleId);

    /**
     * 根据文章id更新文章访问次数
     *
     * @param articleId
     * @return
     */
    @Update("update t_article set visitCount = visitCount + 1 where id = #{articleId}")
    void updateVisitCount(Long articleId);

    /**
     * 根据文章id更新文章
     *
     * @param id
     * @param title
     * @param content
     * @return
     */
    @Update("update t_article set title = #{title}, content = #{content}, updateTime = now() where id = #{id}")
    void modify(Long id, String title, String content);

    /**
     * 根据文章id获取文章
     *
     * @param id
     * @return
     */
    @Select("select * from t_article where id = #{id}")
    Article getByIdArticle(Long id);

    /**
     * 根据文章id点赞
     *
     * @param id
     * @return
     */
    @Update("update t_article set likeCount = likeCount + 1, updateTime = now() where id = #{id}")
    void thumbsUp(Long id);

    /**
     * 根据文章id删除文章
     *
     * @param id
     * @return
     */
    @Update("update t_article set deleteState = 1 where id = #{id}")
    Integer delete(Long id);

    /**
     * 根据文章id更新回复数
     *
     * @param id
     * @return
     */
    @Update("update t_article set replyCount = replyCount + 1, updateTime = now() where id = #{id}")
    Integer updateReplyCount(Long id);

    /**
     * 根据用户id获取所有文章
     *
     * @param id
     * @return
     */
    @Select("select * from t_article where userId = #{id} and deleteState = 0")
    List<Article> getAllByUserId(Long id);

    /**
     * 根据文章id取消点赞
     *
     * @param id
     * @return
     */
    @Update("update t_article set likeCount = likeCount - 1, updateTime = now() where id = #{id}")
    void thumbsOf(Long id);
}

